package com.windsea.relationshipmining.activity;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.windsea.relationshipmining.R;
import com.windsea.relationshipmining.database.DBUserData;
import com.windsea.relationshipmining.model.Relationship;

public class ItemContactDialog extends Dialog implements OnClickListener {

	// private static final String TAG = "ItemContactDialog";
	Button btnOK;
	RadioGroup rgDiffAge, rgFb, rgSeen, rgGender;
	CheckBox cbFamily, cbWork, cbAcquaintance, cbFriend, cbOther;
	RadioButton rbOlder, rbSame, rbYounger, rbYes, rbNo, rbMan, rbWoman,
			rbFreSeen, rbRareSeen;
	int family = 0, work = 0, acquaintance = 0, friend = 0, other = 0, age = 0,
			facebookfriend = 0, seen = 0;
	String gender = "man";
	TextView tvNum;
	boolean isOk = false;
	ArrayList<Relationship> arr;
	RelationshipAdapter adapter;
	String contactid;
	DBUserData dbUserData = null;
	Cursor cur = null;
	Context context;

	public ItemContactDialog(Context context, String contactid,
			ArrayList<Relationship> arr, TextView tvNum, boolean isOk,
			RelationshipAdapter adapter) {
		super(context);
		this.context = context;
		this.contactid = contactid;
		this.tvNum = tvNum;
		this.isOk = isOk;
		this.arr = arr;
		this.adapter = adapter;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Open DBContact
		dbUserData = new DBUserData(this.context);
		dbUserData.open();

		setContentView(R.layout.dialog_itemcustom);
		// this.findViewById(android.R.id.title).setVisibility(View.GONE);

		rgDiffAge = (RadioGroup) findViewById(R.id.rgDiffAge);
		rgFb = (RadioGroup) findViewById(R.id.rgFBFriend);
		rgGender = (RadioGroup) findViewById(R.id.rgGender);
		rgSeen = (RadioGroup) findViewById(R.id.rgSeen);
		cbFamily = (CheckBox) findViewById(R.id.rbFamily);
		cbWork = (CheckBox) findViewById(R.id.rbWork);
		cbAcquaintance = (CheckBox) findViewById(R.id.rbAcquaintance);
		cbFriend = (CheckBox) findViewById(R.id.rbFriend);
		cbOther = (CheckBox) findViewById(R.id.rbOther);

		rbOlder = (RadioButton) findViewById(R.id.rbOlder);
		rbSame = (RadioButton) findViewById(R.id.rbSameAge);
		rbYounger = (RadioButton) findViewById(R.id.rbYounger);
		rbYes = (RadioButton) findViewById(R.id.rbYes);
		rbNo = (RadioButton) findViewById(R.id.rbNo);
		rbMan = (RadioButton) findViewById(R.id.rbmanBtn);
		rbWoman = (RadioButton) findViewById(R.id.rbwomanBtn);
		rbFreSeen = (RadioButton) findViewById(R.id.rbFrequenSeen);
		rbRareSeen = (RadioButton) findViewById(R.id.rbRarelySeen);
		btnOK = (Button) findViewById(R.id.btnQuestionareOK);
		btnOK.setOnClickListener(this);

		// Get database checked?
		this.cur = dbUserData.getContact(contactid);
		if (this.cur.moveToFirst()) {
			setTitle(this.cur.getString(cur.getColumnIndex("Name")));
			this.family = this.cur.getInt(cur.getColumnIndex("Family"));
			this.acquaintance = this.cur.getInt(cur
					.getColumnIndex("Acquaintance"));
			this.work = this.cur.getInt(cur.getColumnIndex("Work"));
			this.friend = this.cur.getInt(cur.getColumnIndex("Friend"));
			this.other = this.cur.getInt(cur.getColumnIndex("Other"));
			this.facebookfriend = this.cur.getInt(cur
					.getColumnIndex("FacebookFriend"));
			this.age = this.cur.getInt(cur.getColumnIndex("DiffAge"));
			this.gender = this.cur.getString(cur.getColumnIndex("Gender"));
			this.seen = this.cur.getInt(cur.getColumnIndex("Seen"));
		}
		setChecked(cbFamily, this.family);
		setChecked(cbAcquaintance, this.acquaintance);
		setChecked(cbFriend, this.friend);
		setChecked(cbWork, this.work);
		setChecked(cbOther, this.other);
		switch (this.age) {
		case -1:
			rbYounger.setChecked(true);
			break;
		case 0:
			rbSame.setChecked(true);
			break;
		case 1:
			rbOlder.setChecked(true);
			break;
		}
		switch (facebookfriend) {
		case 1:
			rbYes.setChecked(true);
			break;
		case 0:
			rbNo.setChecked(true);
			break;
		}
		switch (seen) {
		case 1:
			rbFreSeen.setChecked(true);
			break;
		case 0:
			rbRareSeen.setChecked(true);
			break;
		}
		// --------------------------------

		if (gender.equals("man")) {
			rgGender.check(R.id.rbmanBtn);
		} else {
			rgGender.check(R.id.rbwomanBtn);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == btnOK) {

			this.family = isChecked(cbFamily);
			this.acquaintance = isChecked(cbAcquaintance);
			this.work = isChecked(cbWork);
			this.friend = isChecked(cbFriend);
			this.other = isChecked(cbOther);

			int idChecked1 = rgDiffAge.getCheckedRadioButtonId();
			switch (idChecked1) {
			case R.id.rbOlder:
				this.age = 1;
				break;
			case R.id.rbSameAge:
				this.age = 0;
				break;
			case R.id.rbYounger:
				this.age = -1;
				break;
			}

			int idChecked2 = rgFb.getCheckedRadioButtonId();
			switch (idChecked2) {
			case R.id.rbYes:
				this.facebookfriend = 1;
				// Log.d(TAG, "is FB Friend");
				break;
			case R.id.rbNo:
				this.facebookfriend = 0;
				break;
			}

			int idChecked3 = rgSeen.getCheckedRadioButtonId();
			switch (idChecked3) {
			case R.id.rbFrequenSeen:
				this.seen = 1;
				// Log.d(TAG, "is FB Friend");
				break;
			case R.id.rbRarelySeen:
				this.seen = 0;
				break;
			}
			int idChecked4 = rgGender.getCheckedRadioButtonId();
			switch (idChecked4) {
			case R.id.rbmanBtn:
				this.gender = "man";
				break;
			case R.id.rbwomanBtn:
				this.gender = "woman";
				break;
			}
			// Log.d(TAG, this.contactid);
			dbUserData.insertRelation(this.contactid, this.family, this.work,
					this.acquaintance, this.friend, this.other, this.age,
					this.facebookfriend, this.gender, this.seen);
			this.arr = new ArrayList<Relationship>();

			Cursor mCursor = null;

			mCursor = dbUserData.getContact();
			String label = null;
			if (mCursor != null && mCursor.getCount() != 0
					&& mCursor.moveToFirst()) {

				do {
					if (mCursor.getInt(mCursor.getColumnIndex("NumOfCall")) == 1
							|| mCursor.getInt(mCursor
									.getColumnIndex("NumOfSMS")) == 1) {

						if (mCursor.getInt(mCursor.getColumnIndex("Family")) == 1
								|| mCursor.getInt(mCursor
										.getColumnIndex("Work")) == 1
								|| mCursor.getInt(mCursor
										.getColumnIndex("Acquaintance")) == 1
								|| mCursor.getInt(mCursor
										.getColumnIndex("Friend")) == 1
								|| mCursor.getInt(mCursor
										.getColumnIndex("Other")) == 1) {
							label = "Đã gán nhãn";
						} else {
							label = null;
						}
						arr.add(new Relationship(mCursor.getString(mCursor
								.getColumnIndex("ContactID")), mCursor
								.getString(mCursor.getColumnIndex("Name")),
								label));
					}
				} while (mCursor.moveToNext());
			}

			int count = 0;
			if (arr != null) {
				for (int i = 0; i < arr.size(); i++) {
					if (arr.get(i).getContactRelation() != null) {
						count++;
					}
				}
			}
			if (count != 0 && count == arr.size()) {
				isOk = true;
			} else {
				isOk = false;
			}
			this.tvNum.setText("Đã gán nhãn: " + count + "/ " + arr.size()
					+ " liên hệ");
			adapter.clearAdapter();
			adapter.addAll(arr);
			dbUserData.close();
			dismiss();
		}
	}

	private int isChecked(CheckBox checkbox) {
		if (checkbox.isChecked())
			return 1;
		return 0;
	}

	private void setChecked(CheckBox checkbox, int k) {
		if (k == 1) {
			checkbox.setChecked(true);
		} else {
			checkbox.setChecked(false);
		}
	}

}
